<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>JSMPEG</title>
    <style>
      body {
        background-color: black;
      }
    </style>
  </head>
  <body>
    <button id="createPlayer">add Video</button>
    <script type="module">
      import Player from './client.js';
      Player.WORKER_PATH = './jsmpeg.worker.js';
      Player.warnup();

      const create = document.getElementById('createPlayer');

      create.onclick = function createPlayer() {
        console.log('create player');
        /** @type {HTMLVideoELement} */
        let video;
        const player = new Player({
          url: 'ws://localhost:8082',
          attachCanvas: v => {
            if (video == null) {
              document.body.appendChild(v);
            } else {
              video.parentElement.replaceChild(v, video);
            }
            video = v;
          },
        });

        video.ondblclick = () => {
          player.destroy();
          if (video) {
            video.remove();
          }
        };
      };
    </script>
  </body>
</html>
